package com.liveneo.adapter.mobile.service.impl;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmUploadImage;
import com.liveneo.adapter.base.mapper.PrpmSurveyUserMapper;
import com.liveneo.adapter.base.mapper.PrpmUploadImageMapper;
import com.liveneo.adapter.mobile.model.request.OcrImageRequest;
import com.liveneo.adapter.mobile.service.MobileOcrImageService;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.DateUtils;

/**
 * @Desc：ocr实习层
 * @Title：MobileOcrImageServiceImpl
 * @address: http://www.liveneo.com.cn/
 * @author: zhaoyy
 * @version: 1.0
 * @date: 2016年10月10日 上午11:05:22
 */
@Service
public class MobileOcrImageServiceImpl implements MobileOcrImageService {
    // @Autowired
    // private PrpmUploadOcrImgMapper uploadOcrImgMapper;
    @Autowired
    private PrpmUploadImageMapper uploadOcrImgMapper;
    @Autowired
    private PrpmSurveyUserMapper  surveyUserMapper;

    @Override
    public int saveOcrImage(OcrImageRequest request) {
        PrpmUploadImage ocrImg = new PrpmUploadImage();
        ocrImg.setUploadImageId(UUIDUtils.getUUIDStr());
        ocrImg.setUserCode(request.getUserId());
        ocrImg.setCaseInfoId(request.getCaseUuid());
        ocrImg.setLossItemId(request.getLossUuid());
        ocrImg.setCaseNo(request.getCaseNo());
        ocrImg.setImgName(request.getImgName());
        ocrImg.setImgPath(request.getImgPath());
        ocrImg.setImgType(request.getImgType());
        String shotPlace = request.getShotPlace();
        if (StringUtils.isNotBlank(shotPlace) && shotPlace.contains(",")) {
            String[] strs = StringUtils.split(shotPlace, ",");
            ocrImg.setShotLatitude(strs[0]);
            if (strs.length > 1) {
                ocrImg.setShotLongitude(strs[1]);
            }
        }
        ocrImg.setShotTime(DateUtils.strDateToDate(request.getShotTime()));
        ocrImg.setLockFlag(Integer.parseInt(request.getLockFlag()));
        ocrImg.setOssKey(request.getOSSKey());
        ocrImg.setStayUpload("1");
        ocrImg.setUploadTime(new Date());
        ocrImg.setLicenseNo(request.getLicenseNo());
        if (StringUtils.isNotBlank(request.getTel())) {
            ocrImg.setMobile(request.getTel());
        }
        if ("B".equals(request.getProjectCode())) {
            PrpmSurveyUser surveyUser = surveyUserMapper.findByUserCode(request.getUserCode());
            if (surveyUser != null) {
                ocrImg.setCreateBy(surveyUser.getSurveyName());
            }
        } else {
            ocrImg.setCreateBy(request.getUserCode());
        }
        ocrImg.setCreateTime(new Date());
        return uploadOcrImgMapper.insert(ocrImg);
    }
}
